/*
 * Copyright (C) [2004, 2005, 2006], Hyperic, Inc.
 * This file is part of SIGAR.
 * 
 * SIGAR is free software; you can redistribute it and/or modify
 * it under the terms version 2 of the GNU General Public License as
 * published by the Free Software Foundation. This program is distributed
 * in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
 * even the implied warranty of MERCHANTABILITY or FITNESS FOR A
 * PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
 * USA.
 */

package physicalmachine;

import org.hyperic.sigar.SigarException;
import org.hyperic.sigar.FileSystem;
import org.hyperic.sigar.FileSystemMap;
import org.hyperic.sigar.FileSystemUsage;
import org.hyperic.sigar.NfsFileSystem;
import org.hyperic.sigar.cmd.Shell;
import org.hyperic.sigar.cmd.SigarCommandBase;
import org.hyperic.sigar.shell.FileCompleter;
import org.hyperic.sigar.util.GetlineCompleter;
import static utils.Constants.*;

/**
 * Responsible for obtaining local Hard disk information
 */
public class HardDisk extends SigarCommandBase {

    private long hardDiskSpace;
    private long hardDiskUsedSpace;
    private long hardDiskFreeSpace;
    private String hardDiskFileSystem;
    private GetlineCompleter completer;
    private static final String OUTPUT_FORMAT =
        "%-15s %4s %4s %5s %4s %-15s %s";

    public HardDisk(Shell shell) {
        super(shell);
        setOutputFormat(OUTPUT_FORMAT);
        this.completer = new FileCompleter(shell);
    }

    public HardDisk() {
        super();
        setOutputFormat(OUTPUT_FORMAT);
    }

    public void output(String[] args) throws SigarException {
        if (args.length == 1) {
            FileSystemMap mounts = this.proxy.getFileSystemMap();
            String name = FileCompleter.expand(args[0]);
            FileSystem fs = mounts.getMountPoint(name);
            if (fs != null) {
                output(fs);
                return;
            }
            throw new SigarException(args[0] +" No such file or directory");
        }
        else {
            FileSystem[] fslist = this.proxy.getFileSystemList();
            for (int i=0; i<fslist.length; i++) {
                output(fslist[i]);
            }
        }
    }

    /**
     * Responsible for obtaining and setting the total, full free and full used disk space
     * @param args
     * @throws SigarException
     */
    public void output(FileSystem fs) throws SigarException {
        long used, avail, total;
        try {
            FileSystemUsage usage;
            if (fs instanceof NfsFileSystem) {
                NfsFileSystem nfs = (NfsFileSystem)fs;
                if (!nfs.ping()) {
                    println(nfs.getUnreachableMessage());
                    return;
                }
            }
            usage = this.sigar.getFileSystemUsage(fs.getDirName());
            used = usage.getTotal() - usage.getFree();
            avail = usage.getAvail();
            total = usage.getTotal();
            hardDiskSpace+=total/1024/1024;
            hardDiskUsedSpace+=used/1024/1024;
            hardDiskFreeSpace+=avail/1024/1024;
            hardDiskFileSystem = fs.getSysTypeName();
        } catch (SigarException ex) {
            used = avail = total =0;
        }
        
      }

     /**
     * Responsible for setting all the local disk information
     */
    public void getDiskInfo() {
        try {
            output(new String[0]);
        } catch (SigarException ex) {
            System.err.println(ERROR_MESSAGE+"getting the local disk information: "+ex.getMessage());
        }
    }

    public static void main(String[] args) throws Exception {
        new HardDisk().processCommand(args);
    }

    /**
     * @return the HardDiskSpace
     */
    public long getHardDiskSpace() {
        return hardDiskSpace;
    }

    /**
     * @return the HardDiskUsedSpace
     */
    public long getHardDiskUsedSpace() {
        return hardDiskUsedSpace;
    }

    /**
     * @return the HardDiskFreeSpace
     */
    public long getHardDiskFreeSpace() {
        return hardDiskFreeSpace;
    }

    /**
     * @return the HardDiskFileSystem
     */
    public String getHardDiskFileSystem() {
        return hardDiskFileSystem;
    }
}//end of HardDisk